import random

import  pygame
from game_items import *
from game_hub import  *
from game_music import *
class Game(object):
    ''' 游戏类 '''
    def __init__(self):
        # 游戏窗口
        self.main_window = pygame.display.set_mode(SCREEN_RECT.size)
        pygame.display.set_caption("韩狗大战")
        # 游戏状态窗口
        self.is_game_over = False
        self.is_pause = False



        #精灵组属性
        self.all_group = pygame.sprite.Group()          #所有精灵组
        self.enemies_group = pygame.sprite.Group()      #敌对精灵组
        self.supplies_group = pygame.sprite.Group()     #道具精灵组

        #创建精灵
        #背景精灵,向下方移动
        GameSprite("background.png",1,self.all_group)
        # 背景精灵，交替滚动
        self.all_group.add(Background(False), Background(True))
        #英雄精灵,静止不动
        hero = GameSprite(["me%d.png" % i for i in range(1,3)] ,self.all_group)
        hero.rect.center = SCREEN_RECT.center           #显示在屏幕中间

        #指示器面板
        self.hud_panel = HudPanel(self.all_group)





    def reset_game(self):
        '''  重置游戏 '''
        self.is_game_over = False
        self.is_pause = False
        self.hud_panel.reset_panel()



    def event_handler(self):
        '''事件监听
        :return:如果监听到退出事件,返回True,否则返回False
        '''
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                return True
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                return True
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                if self.is_game_over:            #游戏已经结束
                    self.reset_game()               #重新开始游戏
                else:
                    self.is_pause = not self.is_pause       #切换暂停状态
                # 判断是否在游戏

            if not self.is_game_over and self.is_pause:
                # 监听玩家按下b键，引爆一颗炸弹
                if event.type == pygame.KEYDOWN and event.key == pygame.K_b:
                    # TODO测试炸弹数量变化
                    self.hud_panel.show_bomb(random.randint(0, 100))
                    # TODO测试生命计数数量变化
                    self.hud_panel.lives_count = random.randint(0, 10)
                    self.hud_panel.show_lives()



        return False



    def start(self):
        ''' 开始游戏'''
        frame_counter = 0
        clock = pygame.time.Clock()                 #游戏时钟
        while True:
            #修改逐帧动画计数器
            frame_counter = (frame_counter+1) % FRAME_INTERVAL
            #更新 all_group 中所有精灵内容
            self.all_group.update(frame_counter ==0)

            self.is_game_over =self.hud_panel.lives_count == 0
            if self.event_handler():                #事件监听
                self.hud_panel.save_best_score()
                return
            #判断游戏状态
            if self.is_game_over:
                self.hud_panel.panel_pause(True,self.all_group)

            elif self.is_pause:
                self.hud_panel.panel_pause(False,self.all_group)

            else:
                self.hud_panel.panel_resume(self.all_group)
                #测试修改游戏得分
                if self.hud_panel.increase_score(100):
                    print("升级到 %d" %self.hud_panel.level)
                # 更新 all_group 中所有精灵内容
                self.all_group.update()

            # 绘制all_group中的精灵
            self.all_group.draw(self.main_window)
            pygame.display.update()                 #更新显示
            clock.tick(60)                          #设置刷新帧率


if __name__ == '__main__':
    pygame.init()
    Game().start()
    pygame.quit()